// https://github.com/oozcitak/xmlbuilder-js/wiki/Examples

var builder = require('xmlbuilder');

// var feedObj = { 
//   'launch': {
//     'group':[
//         {"node":{'@name':"turtle",}},
//         {"node":{'@name':"turtle"}},
//     ]
//   }
// }

var launchObj = {
    'launch': {}
}

var Gazbo = function (root) {
    // launch文件的根标签
    this.launch = root.launch
    // world下的对象
    this.objs = []

    this.addObj = function (obj) {
        this.objs.add(obj)
    }
    this.gen_empty_world = function () {
        var arg = [
            { '@name': "paused", '@default': 'false' },
            { '@name': "use_sim_time", '@default': 'true' },
            { '@name': "gui", '@default': 'true' },
            { '@name': "headless", '@default': 'false' },
            { '@name': "debug", '@default': 'false' },
        ]
        var include = {
            '@file': '$(find gazebo_ros)/launch/empty_world.launch',
            'arg': [
                { "@name": 'debug', "@value": "$(arg debug)" },
                { "@name": 'gui', "@value": "$(arg gui)" },
                { "@name": 'paused', "@value": "$(arg paused)" },
                { "@name": 'use_sim_time', "@value": "$(arg use_sim_time)" },
                { "@name": 'headless', "@value": "$(arg headless)" },
            ]
        }
        var param = {'@name':"robot_description",'@command':"$(find xacro)/xacro --inorder '$(find probot_description)/urdf/PROBOT_Anno/probot_anno.xacro'"}

        var node = {
            '@name':"urdf_spawner",
            '@pkg':"gazebo_ros",
            '@type':"spawn_model",
            '@respawn':"false",
            '@output':"screen",
            '@args':"-urdf -model probot_anno -param robot_description",
        }

        // 保证有include节点
        if (!this.launch['arg']) {
            this.launch["arg"] = []
        }
        this.launch.arg.push(arg)
        if (!this.launch['include']) {
            this.launch["include"] = []
        }
        this.launch.include.push(include)
        if (!this.launch['param']) {
            this.launch["param"] = []
        }
        // this.launch.param.push(param)
        // if (!this.launch['node']) {
        //     this.launch["node"] = []
        // }
        // this.launch.node.push(node)
    }
}

var gazbo = new Gazbo(launchObj)
gazbo.gen_empty_world()
console.log(launchObj);
var result = builder.create(launchObj, { encoding: 'utf-8' })
console.log(result.end({ pretty: true }));